package com.xuecheng.orders.api;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.xuecheng.orders.config.AlipayConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Mr.M
 * @version 1.0
 * @description 测试支付宝接口
 * @date 2022/10/20 22:19
 */
@Controller //返回对象--非Json
public class PayTestController {

 @Value("${pay.alipay.APP_ID}")
 String APP_ID;
 @Value("${pay.alipay.APP_PRIVATE_KEY}")
 String APP_PRIVATE_KEY;

 @Value("${pay.alipay.ALIPAY_PUBLIC_KEY}")
 String ALIPAY_PUBLIC_KEY;



 @RequestMapping("/alipaytest")
 public void doPost(HttpServletRequest httpRequest,
                    HttpServletResponse httpResponse) throws ServletException, IOException, AlipayApiException {
  AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, APP_ID, APP_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE);
  //获得初始化的AlipayClient
  AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
//        alipayRequest.setReturnUrl("http://domain.com/CallBack/return_url.jsp");
//        alipayRequest.setNotifyUrl("http://domain.com/CallBack/notify_url.jsp");//在公共参数中设置回跳和通知地址
  alipayRequest.setBizContent("{" +
          "    \"out_trade_no\":\"202410100010101003\"," +
          "    \"total_amount\":4699.00," +
          "    \"subject\":\"Iphone16 16G\"," +
          "    \"product_code\":\"QUICK_WAP_WAY\"" +
          "  }");//填充业务参数
  String form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
  httpResponse.setContentType("text/html;charset=" + AlipayConfig.CHARSET);
  httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
  httpResponse.getWriter().flush();
 }




}